import numpy as np
import taichi as ti

@ti.kernel
def copy_img(ti_img:ti.template(),np_img:ti.types.ndarray()):# pylint: disable=no-value-for-parameter
    for i,j in ti_img:
        ti_img[i,j] = ti.Vector([np_img[i,j,0]/255.,
                                np_img[i,j,1]/255.,
                                np_img[i,j,2]/255.,
                                np_img[i,j,3]/255.])

@ti.kernel
def copy_2d(src:ti.template(),des:ti.template()):# pylint: disable=no-value-for-parameter
    for i,j in src:
        des[i,j] = src[i,j]

@ti.kernel
def bit_blt(src:ti.template(),des:ti.template(),dx:int,dy:int):
    for i,j in src:
        a = src[i,j].a
        des[i+dx,j+dy] = des[i+dx,j+dy] * (1. - a) + src[i,j].rgb * a

def load_img(name):
    img = ti.tools.imread(name)
    img2 = np.ndarray(dtype=np.uint8,shape=img.shape)
    img2[:] = img[:]
    ti_img = ti.Vector.field(4,dtype=ti.f32,shape=(img2.shape[0],img2.shape[1]))
    copy_img(ti_img,img2)
    return ti_img

def load_font():
    font_img = load_img("./data/18.png")
    ascii_rc = ti.Vector.field(4,int,(256,))
    def for_rc(bi,ei,x,y,dw=14,dh=14,H = font_img.shape[1]):
        for i in range(bi,ei):
            xx = x+(i-bi) * dw
            ascii_rc[i] = ti.Vector([xx,H-(y+dh),xx+dw,H-y])

    y = 4
    h = 18
    for_rc(65,68,2,y,14,h) #A-C
    for_rc(68,72,45,y,13,h) #E-G
    for_rc(72,73,99,y,13,h) #H
    for_rc(73,74,114,y,6,h) #I
    for_rc(74,75,120,y,11,h) #J
    for_rc(75,76,133,y,12,h) #K
    for_rc(76,77,148,y,11,h) #L
    for_rc(77,78,160,y,15,h) #M
    for_rc(78,79,177,y,13,h) #N
    for_rc(79,80,191,y,15,h) #O
    for_rc(80,81,207,y,12,h) #P
    for_rc(81,82,220,y,13,h) #Q
    for_rc(82,83,235,y,12,h) #R
    for_rc(83,84,248,y,12,h) #S
    for_rc(84,85,261,y,13,h) #T
    for_rc(85,86,275,y,13,h) #U
    for_rc(86,87,289,y,13,h) #V
    for_rc(87,88,303,y,18,h) #W
    for_rc(88,89,322,y,12,h) #X
    for_rc(89,90,335,y,14,h) #Y
    for_rc(90,91,350,y,12,h) #Z

    s = [('a',2),('b',15),('c',27),('d',38),
         ('e',50),('f',62),('g',70),('h',82),
         ('i',94),('j',98),('k',106),('l',118),
         ('m',124),('n',142),('o',154),('p',165),
         ('q',178),('r',190),('s',198),('t',208),
         ('u',217),('v',228),('w',240),('x',257),
         ('y',268),('z',279),(' ',289),(' ',300)]
    for i in range(len(s)-1): # pylint: disable=consider-using-enumerate
        a = s[i]
        b = s[i+1]
        c = a[0].encode(encoding='ascii')[0]
        for_rc(c,c+1,a[1],30,b[1]-a[1],22)

    for_rc(48,58,2,55,dw=12,dh=19) #0-9

    s = [('.',2),(',',9),(';',15),(':',21),('$',27),
        ('#',38),("'",59),('!',56),('"',61),
        ('/',70),('?',76),('%',87),('&',105),
        ('(',121),(')',128),('+',135),('-',146),
        ('*',153),('<',163),('>',175),('{',186),
        ('}',194),('[',201),(']',208),('\\',214),
        ('|',220),('=',225),('-',236),(' ',243)]
    for i in range(len(s)-1): # pylint: disable=consider-using-enumerate
        a = s[i]
        b = s[i+1]
        c = a[0].encode(encoding='ascii')[0]
        for_rc(c,c+1,a[1],82,b[1]-a[1],19)

    for_rc(0,1,249,84,18,20) #check 0
    for_rc(1,2,272,84,18,20) #check 1
    return font_img,ascii_rc
